package cn.zjweu.dao.impl;

import cn.zjweu.dao.OrderDao;
import cn.zjweu.domain.*;
import cn.zjweu.utils.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;

    private void initThree() throws SQLException {
        conn = JDBCUtil.getConnection();
        pst = null;
        rs = null;
    }

    private void initTwo() throws SQLException {
        conn = JDBCUtil.getConnection();
        pst = null;
    }

    @Override
    public int saveOrder(Order order) {
        String sql = "INSERT INTO `order` VALUES(null,?,?,?,?,?,?,?,0,0,?,null)";
//        String sql = "INSERT INTO order VALUES(null,1,2,3,4,5,6,7)";
        try {
            initTwo();
            User user = order.getUser();
            pst = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pst.setString(1, user.getUsername());
            pst.setObject(2, order.getCreateTime());
            pst.setString(3, user.getCity());
            pst.setString(4, user.getAddress());
            pst.setString(5, user.getTrueName());
            pst.setString(6, user.getTelephone());
            pst.setObject(7, order.getCreateTime());
            pst.setObject(8, order.getTotalPrice());
            System.out.println(pst.toString());
            pst.executeUpdate();
            rs = pst.getGeneratedKeys();
            rs.next();
            return rs.getInt(1);
        } catch (SQLException e) {
            return 0;
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
    }

    @Override
    public boolean saveOrderDetails(int pKey, Dish dishList) {
        String sql = "INSERT INTO orderDetails VALUES(?,?,?,?,?)";

        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, pKey);
            pst.setInt(2, dishList.getDishId());
            pst.setString(3, dishList.getDishName());
            pst.setObject(4, dishList.getDishPrice());
            pst.setInt(5, dishList.getCount());
            System.out.println(pst);
            pst.executeUpdate();
            return MessageInfo.SUCCESS;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(pst, conn);
        }
        return MessageInfo.FAIL;
    }

    @Override
    public List<Order> findOrderListByUsername(int start, int pageSize, String username) {
        String sql = "SELECT * FROM `order` WHERE username = ? LIMIT ?,?";
        List<Order> orderList = new ArrayList<>();
        try {
            initThree();
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setInt(2, start);
            pst.setInt(3, pageSize);
            System.out.println(pst);
            rs = pst.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("orderId"));
                User user = new User();
                user.setUsername(username);
                user.setCity(rs.getString("deliverCity"));
                user.setAddress(rs.getString("deliverAddress"));
                user.setTrueName(rs.getString("name"));
                user.setTelephone(rs.getString("telephone"));
                order.setUser(user);
                order.setCreateTime(rs.getString("createTime"));
                order.setDeliverTime(rs.getString("deliverTime"));
                order.setStatus(rs.getInt("status"));
                order.setDiscount(rs.getBigDecimal("discount"));
                order.setTotalPrice(rs.getBigDecimal("totalPrice"));
                order.setDeliverMan(rs.getString("deliverMan"));
                orderList.add(order);
            }
            return orderList;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return null;
    }

    @Override
    public int findCountByUsername(String username) {
        String sql = "SELECT COUNT(*) FROM `order` WHERE username = ?";
        try {
            initThree();
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            System.out.println(pst);
            rs = pst.executeQuery();
            rs.next();
            return rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return 0;
    }

    @Override
    public List<OrderDetails> findOrderDetailsByOrderId(Integer orderId) {
        String sql = "SELECT * FROM orderDetails WHERE orderId = ?";
        List<OrderDetails> orderDetailsList = new ArrayList<>();
        try {
            initThree();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, orderId);
            System.out.println(pst);

            rs = pst.executeQuery();
            while (rs.next()) {
                OrderDetails orderDetails = new OrderDetails();
                orderDetails.setOrderId(orderId);
                orderDetails.setDishId(rs.getInt("dishId"));
                orderDetails.setDishName(rs.getString("dishName"));
                orderDetails.setDishPrice(rs.getBigDecimal("dishPrice"));
                orderDetails.setQuantity(rs.getInt("quantity"));
                orderDetailsList.add(orderDetails);
            }
            return orderDetailsList;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return null;
    }

    @Override
    public void delOrderByUsername(String username) {
        String sql = "DELETE FROM `order` WHERE username = ?";
        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            System.out.println(pst);

            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(pst, conn);
        }
    }

    @Override
    public void delOrderDetailByUsername(String username) {
        String sql = "DELETE FROM orderDetails WHERE orderId IN(" +
                "SELECT orderId FROM `order` WHERE username = ?)";
        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            System.out.println(pst);

            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(pst, conn);
        }
    }

    @Override
    public int findCount() {
        String sql = "SELECT COUNT(*) FROM `order`";
        try {
            initThree();
            pst = conn.prepareStatement(sql);
            System.out.println(pst);
            rs = pst.executeQuery();
            rs.next();
            return rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return 0;
    }

    @Override
    public List<Order> findOrderList(int start, int pageSize) {
        String sql = "SELECT * FROM `order` LIMIT ?,?";
        List<Order> orderList = new ArrayList<>();
        try {
            initThree();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, start);
            pst.setInt(2, pageSize);
            System.out.println(pst);
            rs = pst.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("orderId"));
                User user = new User();
                user.setUsername(rs.getString("username"));
                user.setCity(rs.getString("deliverCity"));
                user.setAddress(rs.getString("deliverAddress"));
                user.setTrueName(rs.getString("name"));
                user.setTelephone(rs.getString("telephone"));
                order.setUser(user);
                order.setCreateTime(rs.getString("createTime"));
                order.setDeliverTime(rs.getString("deliverTime"));
                order.setStatus(rs.getInt("status"));
                order.setDiscount(rs.getBigDecimal("discount"));
                order.setTotalPrice(rs.getBigDecimal("totalPrice"));
                order.setDeliverMan(rs.getString("deliverMan"));
                orderList.add(order);
            }
            return orderList;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return null;
    }

    @Override
    public boolean changeOrderStatusByOrderId(int orderId, int status) {
        String sql = "UPDATE `order` SET status = ? WHERE orderId = ?";
        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, status);
            pst.setInt(2, orderId);
            System.out.println(pst);
            pst.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
